package wyp.library.ui;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import wyp.library.R;

public class WypGuidePagerDelegate extends PagerAdapter implements ViewPager.OnPageChangeListener {

	private Context mContext;
	private ArrayList<View> mViewArray = new ArrayList<View>();
	private ArrayList<ImageView> mDotImageArray = new ArrayList<ImageView>();

	public WypGuidePagerDelegate(Context _context) {
		super();
		this.mContext = _context;
	}

	public ImageView addView(int _index, View _view) {
		this.mViewArray.add(_view);

		ImageView _imageView = new ImageView(this.mContext);
		LinearLayout.LayoutParams _params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		_params.setMargins(10, 0, 0, 0);
		_imageView.setLayoutParams(_params);
		_imageView.setScaleType(ImageView.ScaleType.MATRIX);
		if (_index == 0) {
			_imageView.setImageResource(R.drawable.wyp_page_now);
		} else {
			_imageView.setImageResource(R.drawable.wyp_page);
		}
		this.mDotImageArray.add(_imageView);
		return _imageView;
	}

	@Override
	public int getCount() {
		return mViewArray.size();
	}

	@Override
	public boolean isViewFromObject(View _view, Object _o) {
		return _view == _o;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView(mViewArray.get(position));
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		container.addView(mViewArray.get(position));
		return mViewArray.get(position);
	}

	@Override
	public void onPageScrolled(int i, float v, int i2) {

	}

	@Override
	public void onPageSelected(int i) {
		try {
			this.mDotImageArray.get(i - 1).setImageResource(R.drawable.wyp_page);
		} catch (Exception e) {
		}

		this.mDotImageArray.get(i).setImageResource(R.drawable.wyp_page_now);

		try {
			this.mDotImageArray.get(i + 1).setImageResource(R.drawable.wyp_page);
		} catch (Exception e) {
		}
	}

	@Override
	public void onPageScrollStateChanged(int i) {

	}
}
